package com.kksoft.readcm.activity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.kksoft.readcm.a.Appconfig;
import com.kksoft.readcm.a.ah;
import com.kksoft.readcm.a.ItemAppObservable;
import com.kksoft.readcm.objects.GlobalVar;
import com.kksoft.readcm.objects.l;
import com.kksoft.readcm.objects.n;
import com.kksoft.readcm.pro.R;
import com.kksoft.readcm.views.AutoCompleteScroller;
import com.kksoft.readcm.views.RilEditText;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

public class EditItemActivity extends RilAppActivity
  implements Observer
{
  private TextView a = null;
  private ImageView b = null;
  private EditText c = null;
  private CheckBox d = null;
  private RilEditText e = null;
  private AutoCompleteScroller f = null;
  private boolean g = false;
  private String h = null;
  private l i;
  
  static String a(EditItemActivity edititemactivity)
  {
      return edititemactivity.h;
  }

  static void a(EditItemActivity edititemactivity, int j, int k)
  {
      if(!edititemactivity.e.hasFocus())
      {
          edititemactivity.f.setVisibility(8);
      } else
      {
          AutoCompleteScroller autocompletescroller = edititemactivity.f;
          String s1 = edititemactivity.e.getText().toString();
          autocompletescroller.a(s1, j, k);
      }
  }

  static void b(EditItemActivity edititemactivity)
  {
      edititemactivity.g = true;
  }

  static RilEditText c(EditItemActivity edititemactivity)
  {
      return edititemactivity.e;
  }

  static void d(EditItemActivity edititemactivity)
  {
      int j = edititemactivity.e.getSelectionEnd();
      if(!edititemactivity.g)
      {
    	  String s1 = edititemactivity.e.getText().toString();
          if(s1.length() != 0 && edititemactivity.h != s1)
          {
              edititemactivity.h = s1;
              by by1 = new by(edititemactivity, s1, j);
              Void avoid[] = new Void[0];
              android.os.AsyncTask asynctask = by1.execute(avoid);
          }
      }
      else
      {
    	  edititemactivity.g = false;
      }
  }

  static AutoCompleteScroller e(EditItemActivity edititemactivity)
  {
      return edititemactivity.f;
  }

  static void f(EditItemActivity edititemactivity)
  {
      edititemactivity.f.b(Appconfig.getThis().getItemAppObservable().a().f(), Appconfig.getThis().getItemAppObservable().a().e());
  }

  protected final Intent a(Bundle paramBundle)
  {
    Intent localIntent1 = new Intent(this, LoadingListActivity.class);
    int j = getIntent().getIntExtra("item_id", 0);
    localIntent1.putExtra("item_id", j);
    localIntent1.putExtra("waitingForLoad", 2);
    return localIntent1;
  }

  protected final boolean c_()
  {
    return true;
  }

  protected final Drawable d_()
  {
    return d(true);
  }

  public void onBackPressed()
  {
    Intent localIntent1 = new Intent();
    if (this.d.isChecked())
      localIntent1.putExtra("com.kksoft.readcm.activity.resultMarked", true);
    setResult(-1, localIntent1);
    super.onBackPressed();
  }

  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    int j = getIntent().getIntExtra("item_id", 0);
    if (j != 0)
    {
      l locall = Appconfig.getThis().getItemAppObservable().a().a(j);
      this.i = locall;
    }
    if (this.i == null)
      finish();
    else
    {
      Appconfig.getThis().getItemAppObservable().a().d();
      this.b = (ImageView)findViewById(R.id.edit_favicon);
      this.b.setImageBitmap(Appconfig.getThis().i().a(this.i.h()));
      this.a = (TextView)findViewById(R.id.edit_url);
      this.a.setText(this.i.e().replace("http://", "").replace("https://", ""));
      this.c = (EditText)findViewById(R.id.edit_title);;
      this.c.setText(this.i.l());
      this.f = (AutoCompleteScroller)findViewById(R.id.tagger);
      this.f.a(Appconfig.getThis().getItemAppObservable().a().f(), Appconfig.getThis().getItemAppObservable().a().e());
      //this.f.a(new bu(this));
      this.f.a(new com.kksoft.readcm.views.c(){

		@Override
		public void a() {
			// TODO Auto-generated method stub
			EditItemActivity.d(EditItemActivity.this);
		}

		@Override
		public void a(String s) {
			// TODO Auto-generated method stub
			String s1 = EditItemActivity.c(EditItemActivity.this).getText().toString();
	        if(s1.length() == 0)
	        {
	            RilEditText riledittext = EditItemActivity.c(EditItemActivity.this);
	            String s2 = String.valueOf(s);
	            String s3 = (new StringBuilder(s2)).append(", ").toString();
	            riledittext.setText(s3);
	            int i = s.length() + 2;
	            riledittext.setSelection(i);
	        } else
	        {
	            int j = EditItemActivity.c(EditItemActivity.this).getSelectionStart();
	            int k = s1.substring(0, j).lastIndexOf(",");
	            int l;
	            int i1;
	            int j1;
	            String s4;
	            StringBuilder stringbuilder;
	            String s5;
	            String s6;
	            RilEditText riledittext2;
	            int k1;
	            int l1;
	            int i2;
	            if(k == -1)
	                k = 0;
	            else
	                k++;
	            l = s1.length();
	            if(j == l)
	                i1 = s1.length();
	            else
	                i1 = s1.length() - 1;
	            j1 = s1.substring(j, i1).indexOf(",");
	            if(j1 == -1)
	                j = i1;
	            else
	                j = j + j1 + 1;
	            s4 = String.valueOf(s1.substring(0, k));
	            stringbuilder = (new StringBuilder(s4)).append(" ").append(s).append(", ");
	            s5 = s1.substring(j, i1);
	            s6 = stringbuilder.append(s5).toString();
	            EditItemActivity.c(EditItemActivity.this).setText(s6);
	            riledittext2 = EditItemActivity.c(EditItemActivity.this);
	            k1 = k + 3;
	            l1 = s.length();
	            i2 = k1 + l1;
	            riledittext2.setSelection(i2);
	        }
		}});
      this.e = (RilEditText)findViewById(R.id.edit_tags);;
      this.e.setOnFocusChangeListener(new OnFocusChangeListener(){

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			if (!hasFocus)
		      EditItemActivity.e(EditItemActivity.this).setVisibility(View.GONE);
			else
		    {
		      EditItemActivity.a(EditItemActivity.this, EditItemActivity.c(EditItemActivity.this).getSelectionStart(), EditItemActivity.c(EditItemActivity.this).getSelectionEnd());
		      EditItemActivity.e(EditItemActivity.this).setVisibility(View.GONE);
		    }
		}
    	  
      });
      
      this.e.setOnEditorActionListener(new OnEditorActionListener(){

		@Override
		public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
			// TODO Auto-generated method stub
			EditItemActivity.c(EditItemActivity.this).clearFocus();
			return false;
		}
    	  
      });
      
      this.e.addTextChangedListener(new TextWatcher(){

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
		    EditItemActivity.a(EditItemActivity.this, EditItemActivity.c(EditItemActivity.this).getSelectionStart(), EditItemActivity.c(EditItemActivity.this).getSelectionEnd());
		}
    	  
      });

      this.e.a(new com.kksoft.readcm.views.t(){

		@Override
		public void a(int paramInt1, int paramInt2) {
			// TODO Auto-generated method stub
			EditItemActivity.a(EditItemActivity.this, paramInt1, paramInt2);
		}
    	  
      });
      if (this.i.o() != null)
      {
        this.e.setText(this.i.o());
      }
      this.d = (CheckBox)findViewById(R.id.checkbox_mark);
      float f1 = getResources().getDisplayMetrics().density;
      int k = this.d.getPaddingLeft();
      int m = (int)(f1 * 10.0F + 0.5F) + k;
      int n = this.d.getPaddingTop();
      int i1 = this.d.getPaddingRight();
      int i2 = this.d.getPaddingBottom();
      this.d.setPadding(m, n, i1, i2);
    }
  }

  protected Dialog onCreateDialog(int paramInt)
  {
    switch (paramInt)
    {
    default:
      return super.onCreateDialog(paramInt);
    case 21:
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage(R.string.dg_long_tag)
    			.setIcon(R.drawable.icon)
    			.setNeutralButton("Ok",new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
    				
    			});
    	Dialog dialog=builder.create();
    	dialog.setCanceledOnTouchOutside(true);
    	return dialog;
    }
  }

  public void onPause()
  {
    super.onPause();
    String str1 = this.c.getText().toString();
    String str2 = this.e.getText().toString();
    ItemAppObservable locale1 = Appconfig.getThis().getItemAppObservable();
    l locall1 = this.i;
    locale1.a(locall1, str1,false, true);
    ItemAppObservable locale2 = Appconfig.getThis().getItemAppObservable();
    l locall2 = this.i;
    locale2.b(locall2, str2, false, true);
    if (this.d.isChecked())
    {
    	ItemAppObservable locale3 = Appconfig.getThis().getItemAppObservable();
      l locall3 = this.i;
      locale3.a(locall3, false, true);
    }
  }

  public void onResume()
  {
    super.onResume();
  }

  protected final int getLayoutID()
  {
    return R.layout.activity_edit;
  }

  public void update(Observable paramObservable, final Object paramObject)
  {
	  GlobalVar localh = GlobalVar.a(paramObject);
    if (localh != null)
    {
      runOnUiThread(new Runnable(){

		@Override
		public void run() {
			// TODO Auto-generated method stub
			if (GlobalVar.a(paramObject).a() == 3)
		      EditItemActivity.f(EditItemActivity.this);
		}
    	  
      });
      super.update(paramObservable, paramObject);
    }
  }
}